/**
 *====================================================
 * 文件名称: ReceivablesServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.project.entity.PickupApplicationEntity;
import com.yuexunit.finance.project.entity.ReceivablesEntity;
import com.yuexunit.finance.project.entity.lexicon.ApplicantLoanStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: ReceivablesServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(ReceivablesService.class)
public class ReceivablesServiceImpl implements ReceivablesService {

	@Override
	public Long createReceivables(ReceivablesEntity entity) {
		entity.setReceivablesState(ApplicantLoanStateEnum.WAIT_AUDIT);
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getReceivablesId();
	}

	@Override
	public void updateReceivables(ReceivablesEntity entity) {
		ReceivablesEntity persist = inquireReceivables(entity.getReceivablesId());
		BeanUtils.copyProperties(entity, persist);
		persist.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(persist);
	}

	@Override
	public void renovateReceivables(ReceivablesEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public void disableReceivables(ReceivablesEntity entity) {
		ReceivablesEntity persist = inquireReceivables(entity.getReceivablesId());
		persist.setEnabled(Boolean.FALSE);
		persist.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(persist);
	}

	@Override
	public ReceivablesEntity inquireReceivables(Long receivablesId) {
		return PersistUtil.get(ReceivablesEntity.class, receivablesId);
	}

	@Override
	public ReceivablesEntity inquireReceivablesDetail(Long receivablesId) {
		ReceivablesEntity entity = PersistUtil.get(ReceivablesEntity.class, receivablesId);
		if (entity == null || !entity.getEnabled()) {
			throw new BusinessException("收款信息不存在");
		}
		return entity;
	}

	@Override
	public PageList<ReceivablesEntity> inquireReceivablesPageList(String beginDate, String endDate, Long pickupId, Long financeId, String financeNumber, Long enterpriseId, String enterpriseName, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ReceivablesEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (StringUtils.isNotBlank(beginDate)) {
			criteria.add(Restrictions.gt("createDateTime", DateUtils.formatBeginDay(beginDate)));
		}
		if (StringUtils.isNotBlank(endDate)) {
			criteria.add(Restrictions.lt("createDateTime", DateUtils.formatEndDay(endDate)));
		}
		if (pickupId != null) {
			criteria.add(Restrictions.eq("pickupId", pickupId));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (StringUtils.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public BigDecimal inquirePickupUnConsentReceivablesAmount(PickupApplicationEntity pickup) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ReceivablesEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.add(Restrictions.eq("pickupId", pickup.getPickupId()));
		criteria.add(Restrictions.eq("receivablesState", ApplicantLoanStateEnum.WAIT_AUDIT));
		criteria.setProjection(Projections.projectionList().add(Projections.sum("beneficiaryAmount")));
		List<Object> list = PersistUtil.get(criteria);
		if (list.isEmpty() || list.get(0) == null) {
			return BigDecimal.ZERO;
		}
		return new BigDecimal(list.get(0).toString());
	}

	@Override
	public ReceivablesEntity inquireReceivablesFromPickupId(Long pickupId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ReceivablesEntity.class);
		criteria.add(Restrictions.eq("pickupId", pickupId));
		List<ReceivablesEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty() || list.get(0) == null) {
			return null;
		}
		return list.get(0);
	}

}
